/*------------------------------------------------------------------------------
 * Fortress DS
 * 
 * Copyright (c) 2008 David Panseri
 * distributed under the terms of the GNU General Public License
 -------------------------------------------------------------------------------
 *
 * OAM.cpp
 *
 -----------------------------------------------------------------------------*/

#include <nds.h>
#include <stdio.h>
#include <OAM.h>
#include <string.h>

oam::oam()
{
  m_pCopy = new tOAM;  
}

void oam::updateOAM(void)
{
    //Flush data cache
    DC_FlushAll();
    //copy our local OAM to the actual OAM
    dmaCopyHalfWords(OAM_DMA_CHANNEL, m_pCopy->spriteBuffer, OAM, SPRITE_COUNT*sizeof(SpriteEntry));
}

void oam::initOAM(void)
{
    u16 i;

    //Clear out all sprite attributes
    for (i=0; i<SPRITE_COUNT; i++) 
    {
        m_pCopy->spriteBuffer[i].attribute[0] = ATTR0_DISABLED;
        m_pCopy->spriteBuffer[i].attribute[1] = 0;
        m_pCopy->spriteBuffer[i].attribute[2] = 0;
    }

    //Set all affine transformation matricies to the identity matrix
    for (i=0; i<MATRIX_COUNT; i++) 
    {
        m_pCopy->matrixBuffer[i].hdx = 1 << 8;
        m_pCopy->matrixBuffer[i].hdy = 0;
        m_pCopy->matrixBuffer[i].vdx = 0;
        m_pCopy->matrixBuffer[i].vdy = 1 << 8;
    }

    //wait for a vblank and then update the OAM
    swiWaitForVBlank();
    updateOAM();
}

void oam::setSprite(u8 index, SpriteEntry *pEntry)
{
    SpriteEntry *pSprite = (SpriteEntry*)&m_pCopy->spriteBuffer[index]; 
    memcpy(pSprite,pEntry,sizeof(SpriteEntry));
}

SpriteEntry* oam::getSprite(u8 index)
{
    return &m_pCopy->spriteBuffer[index];
}
tOAM* oam::getCopy(void)
{
    return m_pCopy;
}
